/*
	插件模块
*/
import config from "../config/config.js"
const Lib = function() {
	//构造函数 传入token
}
Lib.prototype = {
	async $post(obj) {
		const _this = this;
		const token = (await _this.chackToken()).data;
		if (token) {
			obj.header = {
				token
			}
		}
		return await new Promise((resolve, reject) => {
			obj.method = "POST";
			//读取缓存token

			if (obj.url instanceof Object && obj.url.base) {
				obj.url = config.baseUrl + obj.url.url;
			} else if (obj.url instanceof Object && !obj.url.base) {
				obj.url = obj.url.url;
			} else {
				obj.url = config.baseUrl + obj.url;
			}
			obj.success = function(res) {
				if (res.statusCode === 500) {
					//500异常
					const errs = _this.errCatch(res, obj);
					reject(res)
				} else {
					resolve(res);
				}

			};
			obj.fail = function(err) {
				//通过异常捕获器
				const errs = _this.errCatch(err, obj);
				reject(errs);
			};
			uni.request(obj)
		})
	},
	async $get(obj) {
		const _this = this;
		const token = (await _this.chackToken()).data;
		if (token) {
			obj.header = {
				token
			}
		}

		return await new Promise((resolve, reject) => {
			obj.method = "GET";

			if (obj.url instanceof Object && obj.url.base) {
				obj.url = config.baseUrl + obj.url.url;
			} else if (obj.url instanceof Object && !obj.url.base) {
				obj.url = obj.url.url;
			} else {
				obj.url = config.baseUrl + obj.url;
			}

			obj.success = function(res) {
				if (res.statusCode === 500) {
					const errs = _this.errCatch(res, obj);
					reject(res)
				} else {
					resolve(res);
				}

			};
			obj.fail = function(err) {
				//通过异常捕获器
				const errs = _this.errCatch(err, obj);
				reject(errs);
			};
			uni.request(obj)
		})
	},async $post2(obj) {
		const _this = this;
		const token = (await _this.chackToken()).data;
		if (token) {
			obj.header = {
				token
			}
		}
		return await new Promise((resolve, reject) => {
			obj.method = "POST";
			//读取缓存token

			if (obj.url instanceof Object && obj.url.base) {
				obj.url = config.baseUrl2 + obj.url.url;
			} else if (obj.url instanceof Object && !obj.url.base) {
				obj.url = obj.url.url;
			} else {
				obj.url = config.baseUrl2 + obj.url;
			}
			obj.success = function(res) {
				if (res.statusCode === 500) {
					//500异常
					const errs = _this.errCatch(res, obj);
					reject(res)
				} else {
					resolve(res);
				}

			};
			obj.fail = function(err) {
				//通过异常捕获器
				const errs = _this.errCatch(err, obj);
				reject(errs);
			};
			uni.request(obj)
		})
	},
	async $get2(obj) {
		const _this = this;
		const token = (await _this.chackToken()).data;
		if (token) {
			obj.header = {
				token
			}
		}

		return await new Promise((resolve, reject) => {
			obj.method = "GET";

			if (obj.url instanceof Object && obj.url.base) {
				obj.url = config.baseUrl2 + obj.url.url;
			} else if (obj.url instanceof Object && !obj.url.base) {
				obj.url = obj.url.url;
			} else {
				obj.url = config.baseUrl2 + obj.url;
			}

			obj.success = function(res) {
				if (res.statusCode === 500) {
					const errs = _this.errCatch(res, obj);
					reject(res)
				} else {
					resolve(res);
				}

			};
			obj.fail = function(err) {
				//通过异常捕获器
				const errs = _this.errCatch(err, obj);
				reject(errs);
			};
			uni.request(obj)
		})
	},
	errCatch(err, obj) {
		uni.hideLoading()
		console.warn('请求异常：', err, obj.method, obj.url);
		const msg = err.data.message;
		if (msg === '登陆过期') {
			//如果是过期则跳转登陆
			uni.navigateTo({
				url: '/pages/login/index'
			})
		}
		if (msg === 'ORDERPAID') {
			uni.showModal({
				title: '提示',
				content: "您已经支付过此订单"
			})

		}
		if (msg === 'invalid total_fee') {
			uni.showModal({
				title: '提示',
				content: "金额错误"
			})

		} else {
			//不是过期则弹窗报错
			// uni.showModal({
			// 	title: '提示',
			// 	content: msg
			// })
		}
		throw new Error()
		return err
	},
	async getlocal() {
		return new Promise((resolve, reject) => {
			uni.getLocation({
				type: 'gcj02',
				success: loaction => {
					resolve(loaction)
				},
				fail: error => {
					reject(error)
				}
			})
		})

	},
	async startPullDownRefresh() {
		return new Promise((resolve, reject) => {
			uni.startPullDownRefresh({
				success: () => {
					resolve()
				},
				fail: () => {
					reject()
				},
			})
		})

	},
	async stopPullDownRefresh() {
		return new Promise((resolve, reject) => {
			uni.stopPullDownRefresh({
				success: () => {
					resolve()
				},
				fail: () => {
					reject()
				},
			})
		})

	},
	async timeOut(fun, timeOut) {
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				fun();
			}, timeOut)
		})

	},
	async chackToken() {
		return new Promise((resolve, reject) => {
			uni.getStorage({
				key: 'token',
				success: function(res) {
					resolve(res)
				},
				fail: function(err) {
					resolve({
						data: ''
					})
				}
			});
		})
	},
	async setToken(token) {
		return new Promise((resolve, reject) => {
			uni.setStorage({
				key: 'token',
				data: token,
				success: function() {
					resolve()
				}
			});
		})
	},
	get width() {
		return uni.getSystemInfoSync().screenWidth
	},
	get info() {
		return uni.getSystemInfoSync()
	},
	get height() {
		return uni.getSystemInfoSync().windowHeight
	}
}
export default Lib
